#include "../common/dispatcher.hpp"
#include "../client/requestor.hpp"
#include "../client/rpc_caller.hpp"
#include <thread>

void onRpcResponse(const Json_rpc::BaseConnection::ptr &conn,Json_rpc::RpcResponse::ptr &msg)
{
    std::cout<<"收到RPC响应"<<std::endl;
    std::string body = msg->serialize();
    std::cout<<body<<std::endl;
}
void onTopicResponse(const Json_rpc::BaseConnection::ptr &conn,Json_rpc::TopicResponse::ptr &msg)
{
    std::cout<<"收到Topic响应"<<std::endl;

    std::string body = msg->serialize();
    std::cout<<body<<std::endl;
}

void callback(const Json::Value &result)
{
    ILOG("result:%d",result.asInt());
}
int main()
{
    auto requestor = std::make_shared<Json_rpc::client::Requestor>();
    auto caller = std::make_shared<Json_rpc::client::RpcCaller>(requestor);

    auto dispatcher = std::make_shared<Json_rpc::Dispatcher>();

    auto rsp_cb = std::bind(&Json_rpc::client::Requestor::onResponse,requestor.get(),std::placeholders::_1,std::placeholders::_2);
    dispatcher->registerHandler<Json_rpc::BaseMessage>(Json_rpc::MType::REP_RPC,rsp_cb);


    // dispatcher->registerHandler<Json_rpc::RpcResponse>(Json_rpc::MType::REP_RPC,onRpcResponse);//注册映射关系
    // dispatcher->registerHandler<Json_rpc::TopicResponse>(Json_rpc::MType::REP_TOPIC,onTopicResponse);//注册映射关系

    auto client = Json_rpc::ClientFactory::create("127.0.0.1",9090);

    auto message_cb =std::bind(&Json_rpc::Dispatcher::onMessage,dispatcher.get(),
        std::placeholders::_1,std::placeholders::_2);

    client->setMessageCallback(message_cb);
    client->connect();


    auto conn=client->connection();
    //同步测试
    // Json::Value params,result;
    // params["num1"]=11;
    // params["num2"]=22;
    // bool ret = caller->call(conn,"Add",params,result);
    // if(ret!=false)
    // {
    //     std::cout<<"result:"<<result.asInt()<<std::endl;
    // }


    // //异步测试
    // Json::Value params,result;
    // Json_rpc::client::RpcCaller::JsonAsyncResponse res_future;
    // params["num1"]=33;
    // params["num2"]=44;
    // bool ret = caller->call(conn,"Add",params,res_future);
    // if(ret!=false)
    // {
    //     result = res_future.get();
    //     std::cout<<"result:"<<result.asInt()<<std::endl;
    // }

    //回调测试
    Json::Value params,result;
    Json_rpc::client::RpcCaller::JsonResponseCallback func= [](const Json::Value &val){  ILOG("result:%d",val.asInt());};
    params["num1"]=33;
    params["num2"]=44;
    bool ret = caller->call(conn,"Add",params,func);


    // auto rpc_req = Json_rpc::MessageFactory::create<Json_rpc::RpcRequest>();
    // rpc_req->setId(Json_rpc::UuidUtil::uuid());
    // rpc_req->setMethod("Add");
    // rpc_req->setMType(Json_rpc::MType::REQ_RPC);
    // Json::Value val;
    // val["num1"]=11;
    // val["num2"]=22;
    // rpc_req->setParams(val);
    // client->send(rpc_req);


    // auto topic_req = Json_rpc::MessageFactory::create<Json_rpc::TopicRequest>();
    // topic_req->setId(Json_rpc::UuidUtil::uuid());
    // topic_req->setMType(Json_rpc::MType::REQ_TOPIC);
    // topic_req->setOptype(Json_rpc::TopicOptype::TOPIC_CREATE);
    // topic_req->setTopicKey("news");
    // topic_req->setTopicMsg("hello");
    // client->send(topic_req);

    std::this_thread::sleep_for(std::chrono::seconds(5));



    client->shutdown();
    return 0;
}